package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李旭
 * @date 2021/10/13 15:54
 * @Description:
 */
@Controller
//Spring RestController 将当前ListController 进行实例化到SpringIOC容器中
// Spring 默认情况：ListController 单例模式 实现 容器只有一份
//5万并发   ListController 仍然是一份  类中的方法也只有一份
//     将方法复制到本地线程中  ThreadLocal  私有
//     将方法复制到本地线程中  ThreadLocal 私有
//     将方法复制到本地线程中  ThreadLocal 私有
//     将方法复制到本地线程中  ThreadLocal
//     将方法复制到本地线程中  ThreadLocal
//     将方法复制到本地线程中  ThreadLocal
//     将方法复制到本地线程中  ThreadLocal
//     将方法复制到本地线程中  ThreadLocal
//     将方法复制到本地线程中  ThreadLocal    5万个
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;//<bean id="listFeignClient"  class

    //https://search.jd.com/search?keyword=手机&qrst=1&wq=%E5%A4%A7%E7%B1%B3&stock=1&stock=1&ev=exbrand_%E9%87%91%E9%BE%99%E9%B1%BC%5E&cid3=2676
    //开始搜索
    @GetMapping("/list.html")
    public String list(Model model, SearchParam searchParam) {
        //JVM  栈  方法  局部变化  方法出口  堆是共享的
        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //1:回显关键词
        model.addAttribute("searchParam", searchParam);
        //2:品牌集合
        model.addAttribute("trademarkList", responseVo.getTrademarkList());
        //3:urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //4:平台属性
        model.addAttribute("attrsList", responseVo.getAttrsList());
        //5:商品集合
        model.addAttribute("goodsList", responseVo.getGoodsList());
        //6: 总页数  当前页
        model.addAttribute("pageNo", responseVo.getPageNo());
        model.addAttribute("totalPages", responseVo.getTotalPages());
        //7:排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);

        //8:条件回显
        //8.1 显示品牌  //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            model.addAttribute("trademarkParam", "品牌:" + trademark.split(":")[1]);
        }
        //8.2:显示平台属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        return "list/index";
    }

    //显示平台属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //平台属性数组
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                //props=23:4G:运行内存 prop
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());

        }
        return null;
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        // order=1:asc  排序规则   2:asc
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] o = order.split(":");
            orderMap.put("type", o[0]);
            orderMap.put("sort", o[1]);
        } else {
            //默认 排序  hotScore  desc
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        }
        return orderMap;
    }

    //追加URL
    private String buildUrlParam(SearchParam searchParam) {
        //企业上班使用 StringBuilder  比 StringBuffer 快 效率高
        //企业上班使用 StringBuilder  比 StringBuffer 不安全
        // Spring容器
        StringBuilder sb = new StringBuilder();
       // sb.append("http://list.gmall.com/list.html?");
        //1:KeyWord
        String keyword = searchParam.getKeyword();
        sb.append("http://list.gmall.com/list.html?keyword=" + keyword);
        //2:品牌  品牌的Id:品牌的名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=" + trademark);
        }
        //3:平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=" + prop);
            }
        }
        //4：一二三级分类  暂时不写了
        return sb.toString();
    }
}
